﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PixelEditor
{
    public partial class AttrEditableLabel : UserControl
    {
        public AttrEditableLabel()
        {
            InitializeComponent();
        }

        //属性值
        //可编辑标签的文本值
        public override string Text { get => label_text.Text; set => label_text.Text = value; }

        //鼠标双击标签时，显示编辑框
        private void OnDoubleClick(object sender, EventArgs e)
        {
            textBox_edit.Visible = true;
            textBox_edit.Text = Text;
        }

        //结束编辑，刷新标签的内容
        void FinishEdit()
        {
            Text = textBox_edit.Text;
            textBox_edit.Visible = false;
        }

        //在编辑框内按下某键后，检查是不是回车键，如果是，则结束编辑，更新内容
        private void OnEditKeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                FinishEdit();
            }
        }

        //当编辑框的焦点离开的时候，更新标签内容
        private void OnEditFocusLeave(object sender, EventArgs e)
        {
            FinishEdit();
        }
    }
}
